package com.example.shopping_mall.fragment;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shopping_mall.CartActivity;
import com.example.shopping_mall.R;
import com.example.shopping_mall.adapter.CartAdapter;
import com.example.shopping_mall.bean.CartBean;
import com.example.shopping_mall.bean.CartInfo;
import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 *购物车fragment
 **/

public class ShoppingMall_Cart_Fragment extends AllFragment {

    List<CartBean> newcartBeans;
    private TextView hejivalue, edit, anniu, deleteGoods,jiesuan;
    public LinearLayout bottemLin, linerLay;
    private RecyclerView Myrecy;
    private CheckBox allcheck;
    private  CartAdapter adapter;
    private boolean isEdit = false;
    private List<CartInfo> dist;

    @Override
    protected void initView() {

        SwipeRecyclerView recyclerView = findViewById( R.id.recycle );

        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));

        List<CartBean> cartBeans = new ArrayList<>();
        newcartBeans = new ArrayList<>();
        adapter = new CartAdapter( requireContext() );

        //1,构建菜单右拉删除
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                //菜单项
                SwipeMenuItem item = new SwipeMenuItem( requireContext() );
                //菜单标题
                item.setText( "删除" );
                //设置菜单标题颜色
                item.setTextColor( Color.WHITE );
                //设置背景颜色
                item.setBackgroundColor( Color.RED );
                //设置菜单标题字体大小
                item.setTextSize( 17 );
                //设置菜单宽度
                item.setWidth( 220 );
                //设置菜单高度
                item.setHeight( ViewGroup.LayoutParams.MATCH_PARENT );
                //将菜单加入到右侧菜单
                rightMenu.addMenuItem( item );
            }
        };
        //2,将菜单构造器添加到swiprecyclerview
        recyclerView.setSwipeMenuCreator( creator );
        //3,添加菜单监听
        recyclerView.setOnItemMenuClickListener( new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                //设置菜单方向
                if (menuBridge.getDirection() == SwipeRecyclerView.RIGHT_DIRECTION) {
                    newcartBeans.remove( adapterPosition );
                    adapter.notifyItemRemoved( adapterPosition );
                    //关闭菜单
                    menuBridge.closeMenu();
                }
            }
        } );

        for (int i = 0; i < 20; i++) {
            CartBean person = new CartBean();
            person.setId( i );
            person.setName( "1111" );
            person.setNum( "2" );
            person.setColor( "iphone13" );
            person.setIcon( R.mipmap.phone );
            person.setPrice( "99" );
            person.setName( "二狗 " + i );
            newcartBeans.add( person );
        }
        adapter.setDataList(newcartBeans);

        recyclerView.setAdapter(adapter);

        adapter.setListener( new CartAdapter.onlistItenClickListener() {
            @Override
            public void onlistItenClick(int postion) {
                //通知适配器
                cartBeans.get(postion).setIsread(true);
                //适配器条目刷新
                adapter.notifyItemChanged(postion);
            }
        } );
        jiesuan=findViewById( R.id.jiesuan );
        deleteGoods = findViewById(R.id.tv_delete_goods);
        edit = findViewById(R.id.tv_edit);
        bottemLin = findViewById(R.id.lay_edit);
        Myrecy = findViewById(R.id.recycle);
        allcheck = findViewById(R.id.allcheck);
        hejivalue = findViewById(R.id.hejivalue);
        //全选的点击事件
        allcheck.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doCheckAll();
            }
        } );
        //管理的点击事件
        edit.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isEdit) {
                    edit.setText("编辑");
                    bottemLin.setVisibility(View.GONE);
                    isEdit = false;
                } else {
                    edit.setText("完成");
                    bottemLin.setVisibility(View.VISIBLE);
                    isEdit = true;
                }
            }
        } );
        //删除事件
        deleteGoods.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dist !=null && dist.size() == 0){
                    return;
                }
                AlertDialog dialog=new AlertDialog.Builder( requireContext() )
                        .setMessage( "是否删除" )
                        .setPositiveButton( "删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                delete();
                                adapter.notifyDataSetChanged();
                            }
                        } ).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).create();
                dialog.show();

                allcheck.setChecked( false );
                hejivalue.setText( "￥0.0" );
                adapter.notifyDataSetChanged();

            }
        } );
        jiesuan.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* Intent intent = new Intent(requireActivity(), FillOrderActivity.class);
                intent.putExtra("id","1");
                intent.putExtra("productId",productId);
                intent.putExtra("productSkuId",productSkuId);
                startActivity(intent);*/
                Intent intent=new Intent(requireActivity(), CartActivity.class);
                startActivity( intent );

            }
        } );






    }


    /*
     * 全选与反选
     * */


    private void doCheckAll() {
        if (dist != null) {
            for (int i = 0; i < dist.size(); i++) {
                dist.get(i).setChoosed(allcheck.isChecked());
            }
            //通知适配器刷新
            adapter.notifyDataSetChanged();
            calculate();
        }
    }
    /*
     * 价格判断
     * */
    private void   calculate(){

    }

    /*
     * 删除
     * */
    private void  delete(){

    }



    @Override
    protected int getLayoutId() {
        return R.layout.fragment_shopping_mall__cart_;
    }
}